package com.itboye.ihomebank.home;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethod;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.HeTongAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.HeTongBean;
import com.itboye.ihomebank.bean.HetongCaoGao;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.db.HeTongDBManager;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.qianyue.OrderFragment;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DoubleDatePickerDialog;
import com.itboye.ihomebank.util.SPUtils;

public class ActivityEditHeTongZuKe extends BaseOtherActivity implements
		Observer {

	TextView add_shap_title_tv;
	ImageView close_icon;
	private String contractNo;
	private String uid;
	private UserPresenter userPresenter;
	private Calendar current = Calendar.getInstance(Locale.CHINA);
	private HetongCaoGao caoGao;
	private ArrayList<EditText> editTexts = new ArrayList<EditText>();
	private LinearLayout li_container;
	private ArrayList<View> views = new ArrayList<View>();
	private String imgId;
	private HeTongDBManager dbManager;
	Button tijiao;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.tijiao:
			long re = dbManager.deleteCaoGao(uid, contractNo,"renter");
			if (re > 0) {
//				ByAlert.alert("删除成功");
			} else {
//				ByAlert.alert("删除失败");
			}
			buildJson();// 生成json数据
			break;
		default:
			break;
		}
	}

	private void buildJson() {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < views.size(); i++) {
			EditText editText = (EditText) views.get(i).findViewById(R.id.edit);
			if (views.get(i).getTag().equals("sign_renter")) {// 房租信息
				map.put(editText.getTag() + "", imgId==null?editText.getTag(R.id.tagNames)+"":imgId);
			} else {
				map.put(editTexts.get(i).getTag() + "", editTexts.get(i)
						.getText().toString());
			}
		}
		Gson gson=new Gson();
		String jsonStr = gson.toJson(map);
		HeTongDBManager dbManager = new HeTongDBManager(this);
		long re = dbManager.addNote(uid, contractNo,"renter", jsonStr);
		if (re > 0) {
//			ByAlert.alert("添加成功" + contractNo);
		} else {
//			ByAlert.alert("添加失败");
		}
		userPresenter.tiJiaoHeTong(uid, contractNo, jsonStr);
		System.out.println(jsonStr+"租客修改合同");
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_hetong;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("修改合同租客");
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		contractNo = getIntent().getStringExtra("contractNo");
		dbManager = new HeTongDBManager(this);
		String jsonStr = dbManager.getCaoGaoStr(uid, contractNo,"renter");
		Gson gson = new Gson();
		caoGao = gson.fromJson(jsonStr, HetongCaoGao.class);
		userPresenter = new UserPresenter(this);
		userPresenter.heTongParams("1",contractNo);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.heTongParams_success) {
				List<HeTongBean> datas = (List<HeTongBean>) entity.getData();
				HeTongAdapter adapter = new HeTongAdapter(this, datas,
						R.layout.item_hetong);
				if(caoGao==null){
					ByAlert.alert("没有查询到草稿信息");
					return;
				}
				Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
				for (int i = 0; i < datas.size(); i++) {
					HeTongBean bean = datas.get(i);
					Integer inputType = bean.getValueType();// 5、整型 1、字符串 4、日期
															// 0、点击进去 2、签字

					final View view = LayoutInflater.from(this).inflate(
							R.layout.item_hetong, null);
					TextView text = (TextView) view.findViewById(R.id.txt);
					EditText editText = (EditText) view.findViewById(R.id.edit);
					switch (inputType) {// 1: 文本、2: 图片、3: 当前时间 4：当前日期 5：数字 ,默认0
					case 1:
						editText.setInputType(InputType.TYPE_CLASS_TEXT);
						break;
					case 2:

						break;
					case 3:

						break;
					case 4:

						break;
					case 5:
						editText.setInputType(InputType.TYPE_CLASS_NUMBER);
						break;

					default:
						break;
					}
					editText.setTag(bean.getKey());
					editText.setHint(bean.getDesc());
					if (bean.getKey().equals("sign_renter")) {// 1
						editText.setEnabled(false);
						editText.setText(caoGao.getSignRenter() == null ? ""
								: "已签名");
						if (caoGao.getSignRenter() != null) {
							editText.setTag(R.id.tagNames,caoGao.getSignRenter());
						}
					}
					if (bean.getKey().equals("phone_renter")) {// 2
						editText.setText(caoGao.getPhoneRenter() == null ? ""
								: caoGao.getPhoneRenter());
						editText.setInputType(InputType.TYPE_CLASS_PHONE);
					}
					if (bean.getKey().equals("sign_renter_year")) {// 3
						editText.setText(current.get(Calendar.YEAR)+"");
						editText.setEnabled(false);
					}
					if (bean.getKey().equals("sign_renter_month")) {// 4
						editText.setText((current.get(Calendar.MONTH) + 1)+"");
						editText.setEnabled(false);
					}
					if (bean.getKey().equals("sign_renter_day")) {// 5
						editText.setText(current.get(Calendar.DATE)+"");
						editText.setEnabled(false);
					}

					editTexts.add(editText);// 将所有的编辑框放进集合

					text.setText(bean.getAlias());
					li_container
							.addView(view, li_container.getChildCount() - 1);
					view.setTag(datas.get(i).getKey() + "");
					views.add(view);
					view.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if ((v.getTag() + "").equals("sign_renter")) {// 甲方签字
								Intent intent = new Intent(
										ActivityEditHeTongZuKe.this,
										ActivityHuaBan.class);
								startActivityForResult(intent, 107);//
							}
						}
					});
				}
			} else if (entity.getEventType() == UserPresenter.heTongParams_fail) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.heTongTiJiao_success) {
				Intent intent=new Intent();
				intent.putExtra("shenfen", "renter");
				intent.putExtra("contractNo", contractNo);
				sendBroadcast(intent);
				OrderFragment.getInstance().refresh();
				ByAlert.alert(entity.getData());
				finish();
			} else if (entity.getEventType() == UserPresenter.heTongTiJiao_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 107 && resultCode == 108) {// 上传签字图片
			imgId = data.getStringExtra("imgId");
			for (View v : views) {
				if (v.getTag().toString().equals("sign_leaser")) {
					v.findViewById(R.id.edit).setVisibility(View.VISIBLE);
					((EditText) v.findViewById(R.id.edit)).setText("已签名");
				}
			}
		}
	}

}
